package br.ufrj.neg.reuniao.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.view.VelocityViewServlet;

import br.ufrj.neg.constants.General;
import br.ufrj.neg.reuniao.bean.ParticipanteBean;
import br.ufrj.neg.reuniao.bean.ReuniaoBean;
import br.ufrj.neg.reuniao.model.ParticipanteModel;
import br.ufrj.neg.reuniao.model.ReuniaoModel;
import br.ufrj.neg.servlet.PageContext;

import com.thoughtworks.xstream.XStream;

public class EditarParticipanteAction extends VelocityViewServlet {

	private static final long serialVersionUID = -1502095409248120929L;

	// private static Logger log = Logger.getLogger("rootLogger");

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		ParticipanteBean pb = new ParticipanteBean();
		ReuniaoBean rb = null;
		try {
			HttpSession session = request.getSession(false);
			synchronized (session) {
				rb = (ReuniaoBean) session.getAttribute("ReuniaoBean");
			}
			Integer indParticipante = null;
			if (request.getParameter("indParticipante") != null)
				indParticipante = Integer.valueOf(request
						.getParameter("indParticipante"));
			
			pb = rb.getParticipantes().get(indParticipante.intValue());
			XStream xStream = new XStream();
			PrintWriter out = response.getWriter();
			String XML = xStream.toXML(pb); 
			response.setContentType("text/xml");
			System.out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + XML);
			out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + XML);
			
		} catch (Exception e) {
			System.out.println(e);
			request.getRequestDispatcher(General.TELA_ERRO_500).forward(request,
					response);
		}
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		ReuniaoModel rm = new ReuniaoModel();
		ParticipanteModel pm = new ParticipanteModel();

		try {

			HttpSession session = request.getSession(false);
			synchronized (session) {
				session.removeAttribute("NegociacaoBean");

			}
			Context context = createContext(request, response);
			fillContext(context, request);

			try {
				Integer idReuniao = Integer.parseInt(request
						.getParameter("idReuniao"));
				setContentType(request, response);
				
				context.put("pageContext", new PageContext(request));
				context.put("reuniao", rm.getReuniao(idReuniao));
				context.put("participantesDataSet",
						pm.listaParticipanteporReuniao(idReuniao));
			} catch (Exception e) {
				// TODO: handle exception
			}

			Template template = getTemplate("/templates/user/sisneg/editarParticipante.vm");
			mergeTemplate(template, context, response);

		} catch (Exception e) {
			System.out.println(e);
			request.getRequestDispatcher(General.TELA_ERRO_500).forward(request,
					response);
		}
	}

}
